package com.zyb.educms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commonutils.R;
import com.zyb.educms.entity.CrmBanner;
import com.zyb.educms.entity.vo.BannerQuery;
import com.zyb.educms.service.CrmBannerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 * 管理员使用接口
 * */
@RestController
@RequestMapping("/educms/admin-banner")
//@CrossOrigin
public class BannerAdminController {

    @Resource
    private CrmBannerService bannerService;

    //current 当前页
    //limit 每页记录数
    @GetMapping("page/{current}/{limit}")
    public R pageListBanner(@PathVariable("current") Long current, @PathVariable("limit") Long limit) {
        Page<CrmBanner> bannerPage = new Page<>(current, limit);
        bannerService.page(bannerPage, null);
        Long total = bannerPage.getTotal();
        List<CrmBanner> rows = bannerPage.getRecords();
        return R.ok().data("total", total).data("rows", rows);
    }

    //添加轮播图
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner banner) {
        bannerService.save(banner);
        return R.ok();
    }

    //根据ID查询一个轮播图
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner", banner);
    }

    //修改轮播图
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    //删除轮播图
    @DeleteMapping("deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id) {
        boolean flag = bannerService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //条件查询带分页
    @PostMapping("pageBannerCondition/{current}/{limit}")
    //使用RequestBody必须使用post提交
    public R pageTeacherCondition(@PathVariable("current") Long current, @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) BannerQuery bannerQuery) {
        Page<CrmBanner> bannerPage = new Page<>(current, limit);
        //构造条件
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        String title = bannerQuery.getTitle();
        String begin = bannerQuery.getBegin();
        String end = bannerQuery.getEnd();
        //多条件进行查询
        // mybatis学过 动态sql
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        //进行排序
        wrapper.orderByDesc("gmt_create");
        bannerService.page(bannerPage, wrapper);
        Long total = bannerPage.getTotal();
        List<CrmBanner> rows = bannerPage.getRecords();
        return R.ok().data("total", total).data("rows", rows);
    }
}
